<?php
declare (strict_types=1);

namespace app\sysuser\controller\core;

use app\common\controller\SysBackend;
use app\common\enums\AccountTypeEnum;
use app\common\model\banxi\core\CoreTrialClassOrder as CoreTrialClassOrderModel;
use app\common\service\core\TrialClassOrderService;
use app\common\service\course\CourseAppointmentService;
use ba\Exception;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Db;

class CoreTrialClassOrder extends SysBackend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new CoreTrialClassOrderModel();
    }


    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', $info);
    }

    public function trialClassAppointment()
    {
        $param = $this->request->param();
        $courseAppointmentService = new CourseAppointmentService();
        $res = $courseAppointmentService->trialClassAppointment($param);
        if (!$res) {
            $this->fail($courseAppointmentService->getError());
        }
        $this->ok();
    }

    /**
     * 创建并支付
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function placeTrialClassOrder()
    {
        $param = $this->request->param();
        if (empty($param)) {
            $this->fail('请传递参数');
        }
        $studentId = $param['student_id'];
        if (empty($studentId)) {
            $this->fail('请设置学生');
        }
        Db::startTrans();
        try {
            $trialClassOrderService = new TrialClassOrderService();
            $orderId = $trialClassOrderService->place($studentId, $param,AccountTypeEnum::PLATFORM);
            $info = $trialClassOrderService->payInitiate($studentId, $orderId, $param['payType'], 'pc');
            Db::commit();
        } catch (Exception|PDOException|ValidateException $e) {
            Db::rollback();
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功', $info);
    }
}
